//
//  html5MYViewController.m
//  LazyCat
//
//  Created by rocky on 15/12/18.
//  Copyright (c) 2015年 zhanshu. All rights reserved.
//

#import "html5MYViewController.h"
#import "NJKWebViewProgress.h"
#import "NJKWebViewProgressView.h"

@interface html5MYViewController ()<UIWebViewDelegate,NJKWebViewProgressDelegate>
{
    NJKWebViewProgressView *_progressView;
    NJKWebViewProgress *_progressProxy;
    BOOL _finishLoad;//web页是否加载完毕
}
@property (nonatomic,strong) UIWebView *webView;
@property (nonatomic,strong) UIView *noNetView;//无网络视图

@end

@implementation html5MYViewController

- (void)backAction:(UIButton *)btn {
    if (![_webView canGoBack]) {//判断是否可以HTML返回
        if (!self.navigationController.viewControllers) {
            [self dismissViewControllerAnimated:YES completion:^{
                
            }];
        }else {
            [self.navigationController popViewControllerAnimated:YES];
 
            
        }
    }else {
        [_webView goBack];
    

        
        
    }
}
-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navBar addSubview:_progressView];
    if (kNetworkNotReachability) {
        [[MessShowView share] showWithMess:@"当前网络不给力"];
        self.webView.hidden = YES;
        self.noNetView.hidden = NO;
    }else {
        self.noNetView.hidden = YES;
        self.webView.hidden = NO;
        //[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.requestUrl]]];
    }
 
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    //导航
    [self initNav:self.titleName doBack:YES];
    UIView *lineV = [[UIView alloc]initWithFrame:CGRectMake(0, 63, ScreenWidth, 1)];
    lineV.backgroundColor = [UIColor lightGrayColor];
    lineV.alpha = 0.2;
    [self.view addSubview:lineV];
    
    
    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cancelBtn.frame = CGRectMake(30, 20, 60, 40);
    cancelBtn.titleLabel.font = [UIFont fontWithName:nil size:15];
    [cancelBtn setTitle:@"关闭" forState:UIControlStateNormal];
    [cancelBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [cancelBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    [self.navBar addSubview:cancelBtn];
    

    // 创建视图
    self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight-64)];
    self.webView.backgroundColor = BackColor;
    self.webView.delegate = self;
    [self.webView.scrollView setBounces:YES];//边缘弹动效果
    [self.webView.scrollView setContentInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    [self loadMessPart];//加载进度条
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.requestUrl]]];// 加载请求
    [self.view addSubview:self.webView];
    
    if ([self.showRefresh isEqualToString:@"1"]) {
        [self createRefresh];
    }
    self.noNetView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight - 104)];
    self.noNetView.hidden = YES;
    self.noNetView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.noNetView];
    UIImageView *netImg = [[UIImageView alloc] initWithFrame:CGRectMake((ScreenWidth-100)/2, 70, 100, 90)];
    netImg.image = [UIImage imageNamed:@"无网络_r1_c1"];
    [self.noNetView addSubview:netImg];
    UILabel *netFail = [[UILabel alloc] initWithFrame:CGRectMake(0, 180, ScreenWidth, 20)];
    netFail.text = @"网络请求失败";
    netFail.textAlignment = NSTextAlignmentCenter;
    netFail.font = [UIFont systemFontOfSize:16];
    [self.noNetView addSubview:netFail];
    UIButton *refresh = [[UIButton alloc] initWithFrame:CGRectMake((ScreenWidth-120)/2, 210, 120, 40)];
    [refresh setImage:[UIImage imageNamed:@"无网络_r3_c2 下午4.23.04"] forState:UIControlStateNormal];
    refresh.adjustsImageWhenHighlighted = NO;
    [self.noNetView addSubview:refresh];
    [refresh addTarget:self action:@selector(refreshV) forControlEvents:UIControlEventTouchUpInside];//刷新
}

-(void)backAction
{
    [self.navigationController popToRootViewControllerAnimated:YES];
    
//    NSArray *backArr = self.navigationController.viewControllers;
//    [self.navigationController popToViewController:backArr[1] animated:YES];
    
}

-(void)refreshV {//刷新按钮
    if (kNetworkNotReachability) {
        [[MessShowView share] showWithMess:@"当前网络不给力"];
        self.webView.hidden = YES;
        self.noNetView.hidden = NO;
    }else {
        self.noNetView.hidden = YES;
        self.webView.hidden = NO;
        [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.requestUrl]]];
    }
}
-(void)createRefresh {// 刷新
    UIButton *shareBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    shareBtn.frame = CGRectMake(ScreenWidth-8-18, 20+(44-18)/2, 20, 20);
    [shareBtn setImage:[UIImage imageNamed:@"refresh.png"] forState:UIControlStateNormal];
    [shareBtn setHitTestEdgeInsets:UIEdgeInsetsMake(-10, -20, -10, -10)];
    [shareBtn addTarget:self action:@selector(refresh) forControlEvents:UIControlEventTouchUpInside];
    [self.navBar addSubview:shareBtn];
}
- (void)loadMessPart {//加载进度条
#pragma mark -- 进度条1
    _progressProxy = [[NJKWebViewProgress alloc]init];
    //_webView.delegate = _progressProxy;
    _webView.delegate = self;
    _progressProxy.webViewProxyDelegate = self;
    _progressProxy.progressDelegate = self;
    CGRect navBounds = self.navBar.bounds;
    CGRect barFrame = CGRectMake(0, navBounds.size.height - 2, 0, 2);
    _progressView = [[NJKWebViewProgressView alloc] initWithFrame:barFrame];
    _progressView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
    [UIView animateWithDuration:1 animations:^{
        _progressView.frame = CGRectMake(_progressView.frame.origin.x, _progressView.frame.origin.y, ScreenWidth - ScreenWidth/2+80, 2);
    } completion:^(BOOL finished) {
        if (_finishLoad) {
            _progressView.hidden = YES;
        }
    }];
}
#pragma mark -- 进度条2
//加载完成
- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [UIView animateWithDuration:1 animations:^{
        _progressView.frame = CGRectMake(_progressView.frame.origin.x, _progressView.frame.origin.y, ScreenWidth, 2);
    } completion:^(BOOL finished) {
        _finishLoad = YES;
        _progressView.hidden = YES;
    }];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    if (kNetworkNotReachability) {
        [[MessShowView share] showWithMess:@"网络不给力"];
        _webView.hidden = YES;
    }
    _progressView.hidden = YES;
}
-(void)refresh {// 刷新
 [_webView reload];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end
